package cn.ecut.controller;

import cn.ecut.dao.AlumniDao;
import cn.ecut.model.Alumni;
import cn.ecut.model.Message;
import cn.ecut.model.Scholarship;
import cn.ecut.model.Student;
import cn.ecut.service.AlumniService;
import cn.ecut.service.ScholarshipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@RequestMapping("/scholarship")
@Controller
public class ScholarshipController {
    @Autowired
    private ScholarshipService scholarshipService;

    @ResponseBody
    @RequestMapping("/selectAllaid")
    public Message selectAllaid(){
        Message message = new Message();
        List<Alumni> list = scholarshipService.selectAllAlumni();
        message.setFlag(true);
        message.setData(list);
        return message;
    }

    @ResponseBody
    @RequestMapping("/addScholarship")
    public Message addScholarship(Scholarship scholarship){
        Message message = new Message();
        scholarship.setApplyNum(0);
        scholarship.setResidue(0);
        int role = scholarshipService.addScholarship(scholarship);
        if(role == 1){
            message.setFlag(true);
        }else {
            message.setFlag(false);
        }
        return message;
    }

    @ResponseBody
    @RequestMapping("/showScholarship")
    public HashMap<String, Object> showScholarship(int page, int limit) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        List<Scholarship> list = new ArrayList<>();
        int offset=(page-1)*limit;
        list = scholarshipService.selectAllScholarship(offset,limit);
        Integer count = scholarshipService.selectAllcount();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", list);//dao层查询返回list数据
        Iterator<Map.Entry<String, Object>> entries = map.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<String, Object> entry = entries.next();
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/updataScholarship")
    public Message updataScholarship(Scholarship scholarship){
        Message message = new Message();
        scholarship.setResidue(scholarshipService.updataResidu(scholarship.getSid(),scholarship.getSum()));
        int role = scholarshipService.updataScholarship(scholarship);
        if(role==1){
            message.setFlag(true);
        }else {
            message.setFlag(false);
            message.setMsg("更新失败");
        }
        return message;
    }

    @ResponseBody
    @RequestMapping("/deleteScholarship")
    public Message deleteScholarship(Integer sid){
        Message message = new Message();
        int role = scholarshipService.deleteBySid(sid);
        if(role==1){
            message.setFlag(true);
        }else {
            message.setFlag(false);
            message.setMsg("删除失败");
        }
        return message;
    }

    @ResponseBody
    @RequestMapping("/selectScholarship")
    public HashMap<String, Object> selectScholarship(String searchResult ,int page ,int limit) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        List<Scholarship> list = new ArrayList<>();
        int offset=(page-1)*limit;
        list = scholarshipService.advanceSelect(searchResult,offset,limit);
        System.out.println(page);
        System.out.println(limit);
        Integer count = scholarshipService.advanceSelectCount(searchResult);
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", list);//dao层查询返回list数据
        Iterator<Map.Entry<String, Object>> entries = map.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<String, Object> entry = entries.next();
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/scholarshipDetial")
    public Message scholarshipDetail(Integer sid){
        Message message = new Message();
        Scholarship scholarship = scholarshipService.selectDetailScholarshipBySid(sid);
        message.setData(scholarship);
        return message;
    }

}
